package com.ly.cloud.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.BadSqlGrammarException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ly.cloud.dto.UserDto;
import com.ly.cloud.entity.UserPo;
import com.ly.cloud.exception.CloudException;
import com.ly.cloud.exception.biz.BusinessException;
import com.ly.cloud.exception.biz.DataNotFoundException;
import com.ly.cloud.exception.mapper.DBException;
import com.ly.cloud.mapper.UserMapper;
import com.ly.cloud.service.UserService;
import com.ly.cloud.vo.UserVo;

/** 
 * Class Name: UserServiceImpl  
 * Description: 
 * @author: WenboLi
 * @mail: wenboli@ly-sky.com 
 * @date: 2018年4月9日
 * @version: 1.0
 *
 */  
@Service
@Transactional(readOnly = true, propagation = Propagation.NOT_SUPPORTED)
public class UserServiceImpl extends ServiceImpl<UserMapper, UserPo> implements UserService {

	@Autowired
	private UserMapper userMapper;

	/**(non-Javadoc)
	 * Title: selectPage
	 * Description: 
	 * 分页查询用户列表
	 * Created On: 2018年4月9日
	 * @author WenboLi
	 * <p>
	 * @param page
	 * @param dto
	 * @return  UserServiceImpl
	 * @see com.ly.cloud.service.UserService#selectPage(com.baomidou.mybatisplus.plugins.Page, com.ly.cloud.dto.UserDto)
	 */
	@Override
	public Page<UserVo> selectPage(Page<UserVo> page, UserDto dto) {
		try {
			return page.setRecords(userMapper.selectPage(page, dto));
		} catch (Exception e) {//注意，这里只是演示，实际情况请捕捉更加详细的异常，判断抛出详细的业务异常，不要将异常的代码直接抛出去了
			throw new CloudException("查询异常，请重试!", e);
		}
	}


	/**(non-Javadoc)
	 * Title: insert
	 * Description: 
	 * Created On: 2018年4月9日
	 * @author WenboLi
	 * <p>
	 * @param user
	 * @return  UserServiceImpl
	 * @see com.ly.cloud.service.UserService(com.ly.cloud.entity.UserPo)
	 */
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)//如果只是单次操作不需要加这个，如果具有多次数据库操作，需要加上
	@Override
	public boolean insert(UserDto user) {
		try {
			UserPo po = new UserPo();
			BeanUtils.copyProperties(user, po);
			int r = userMapper.deleteById("admin1");
			if (!(r == 2)) {
				throw new CloudException("测试回滚");
			}
			int rs = userMapper.insert(po);
			if (rs > 0) {
				return true;
			} else {
				return false;
			} 
		} catch (Exception e) {
			throw new DBException("删除用户失败！", e);
		}
	}


	/**(non-Javadoc)
	 * Title: delete
	 * Description: 
	 * Created On: 2018年4月9日
	 * @author WenboLi
	 * <p>
	 * @param userId
	 * @return  UserServiceImpl
	 * @see com.ly.cloud.service.UserService#delete(java.lang.String)
	 */
	@Override
	public boolean delete(String userId) {
		try {
			int rs = userMapper.deleteById(userId);
			if (rs > 0) {
				return true;
			} else {
				return false;
			} 
		} catch (Exception e) {
			throw new DBException("删除用户失败！", e);
		}
	}


	/**(non-Javadoc)
	 * Title: get
	 * Description: 
	 * Created On: 2018年4月9日
	 * @author WenboLi
	 * <p>
	 * @param userId
	 * @return  UserServiceImpl
	 * @see com.ly.cloud.service.UserService#get(java.lang.String)
	 */
	@Override
	public UserPo get(String userId) {
		try {
			UserPo po = userMapper.selectById(userId);
			return po;
		} catch (Exception e) {
			throw new DBException("获取用户信息失败！", e);
		}
	}


	/**(non-Javadoc)
	 * Title: updateById
	 * Description: 
	 * Created On: 2018年8月21日
	 * @author WenboLi
	 * <p>
	 * @param dto
	 * @return  UserServiceImpl
	 * @see com.ly.cloud.service.UserService#updateById(com.ly.cloud.dto.UserDto)
	 */
	@Override
	public boolean updateById(UserDto dto) {
		try {
			UserPo po = new UserPo();
			BeanUtils.copyProperties(dto, po);
			return retBool(userMapper.updateById(po));
		} catch (Exception e) {
			throw new BusinessException("获取用户信息失败！", e);
		}
	}


	/**(non-Javadoc)
	 * Title: getUserBaseInfo
	 * Description: 
	 * Created On: 2018年8月21日
	 * @author WenboLi
	 * <p>
	 * @param id
	 * @return  UserServiceImpl
	 * @see com.ly.cloud.service.UserService#getUserBaseInfo(java.lang.String)
	 */
	@Override
	public UserVo getUserBaseInfo(String id) {
		try {
			UserPo po = userMapper.selectById(id);
			UserVo vo = new UserVo();
			BeanUtils.copyProperties(po, vo);
			return vo;
		} catch (BeansException e) {
			throw new BusinessException("获取用户基本信息失败！", e);
		} catch (Exception e) {
			throw new BusinessException("获取用户基本信息失败！", e);
		}
	}


	/**(non-Javadoc)
	 * Title: updatePasswordByUserId
	 * Description: 
	 * Created On: 2018年8月21日
	 * @author WenboLi
	 * <p>
	 * @param userId
	 * @param password
	 * @return  UserServiceImpl
	 * @see com.ly.cloud.service.UserService#updatePasswordByUserId(java.lang.String, java.lang.String)
	 */
	@Override
	public boolean updatePasswordByUserId(String userId, String password) {
		try {
			return retBool(userMapper.updatePasswordByUserId(userId, password));
		} catch (BadSqlGrammarException e) {
			throw new DBException("数据库操作异常!", e);
		} catch (Exception e) {
			throw new BusinessException("更新用户密码失败！", e);
		}
	}


	/**(non-Javadoc)
	 * Title: updateUserPassword
	 * Description: 
	 * Created On: 2018年8月21日
	 * @author WenboLi
	 * <p>
	 * @param userId
	 * @param oldPassword
	 * @param newPassword
	 * @return  UserServiceImpl
	 * @see com.ly.cloud.service.UserService#updateUserPassword(java.lang.String, java.lang.String, java.lang.String)
	 */
	@Override
	public boolean updateUserPassword(String userId, String oldPassword, String newPassword) {
		try {
			UserPo user = userMapper.selectById(userId);
			if (user == null) {
				throw new DataNotFoundException("用户账号错误！");
			}
			if (user.getPassword().equals(oldPassword)) {
				return retBool(userMapper.updatePasswordByUserId(userId, newPassword));
			} else {
				throw new BusinessException("新旧密码不匹配！");
			} 
		} catch(DataNotFoundException e) {
			throw e;
		} catch(BusinessException e) {
			throw e;
		} catch (Exception e) {
			throw new BusinessException("更新用户密码失败！", e);
		}
	}


	@Override
	public String testCursorPro() {
		try {
			String msg = "";
			 //1，有返回值的存储过程
	        /*Map<String, String> param = new HashMap<String, String>();
	        param.put("name", "张三");
	        userMapper.testPro(param);
	        System.out.println("-----------testPro----------" + param.get("result")); //获取返回值
	        msg = param.get("result");*/
	        
	        //2，游标返回值的存储过程
	        Map<String, Object> tparam = new HashMap<String, Object>();
	        userMapper.testCursorPro(tparam);
	        List<UserVo> list = (List<UserVo>) tparam.get("v_cursor");  //获取返回游标值
	        for (UserVo user : list) {
	            System.out.println(user.getUserId()+"===="+user.getUserName());
	        }
			return msg;
		} catch (Exception e) {
			throw new BusinessException(e);
		}
	}

}
  